package atguigu.urlconn;

import cn.hutool.core.io.FileUtil;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class UrlTest {

    public static void main(String[] args) {
        URL url = null;
        try {
            url = new URL("http://www.baidu.com");
            URLConnection conn = url.openConnection();
            conn.setConnectTimeout(10000);
            conn.connect();
            InputStream inStream = conn.getInputStream();
            FileUtil.writeFromStream(inStream, new File("d:/instream.txt"));
            ByteArrayOutputStream outstream=new ByteArrayOutputStream();
            byte[] buffer=new byte[1024];
            int len=-1;
            while((len=inStream.read(buffer)) !=-1){
                outstream.write(buffer, 0, len);
            }
            outstream.close();
            inStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
